package core;

import org.easyway.collisions.GroupCollision;
import org.easyway.input.Keyboard;
import static org.easyway.input.Keyboard.isKeyDown;
import org.easyway.interfaces.extended.ILoopable;
import org.easyway.objects.sprites2D.Mask;
import org.easyway.objects.sprites2D.SpriteColl;
import org.easyway.objects.texture.Texture;

@SuppressWarnings("serial")
public class Player extends SpriteColl implements ILoopable {
	
	protected static boolean shotOnScreen;
	protected static Texture image;
	private float speed = 3.0f;
	
	public static void init() {
		if (image != null)
			return;
		image = new Texture("images/player.png", 255, 255, 255);
		new Mask(image);
	}

	public Player(float x, float y) {
		super(x, y, image);
		shotOnScreen = false;
		GroupCollision group = GroupCollision.getGroup("myGroup");
		group.source.add(this);
	}
	
	public void onCollision() {
		destroy();
	}
	
	public void loop() {
		if (isKeyDown(Keyboard.KEY_LEFT) && getX()>0) {
			incX(-speed);
		} else if (isKeyDown(Keyboard.KEY_RIGHT) && getX()+getWidth()<800) {
			incX(speed);
		}
		if (isKeyDown(Keyboard.KEY_SPACE) && !shotOnScreen) {
			new Shot(getX(), getY());
			shotOnScreen = true;
		}
	}
	
	public static void signalShotOut() {
		shotOnScreen = false;
	}
}
